package com.github.tvbox.osc.ui.dialog;

import android.content.Context;

import com.github.tvbox.osc.ui.adapter.SelectDialogAdapter;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import io.github.source.bean.Site;
import io.github.source.config.VodConfig;

/**
 * @ClassName: SiteDialog
 * @Description: TODO(源站点选择)
 * @author: huangrongcai
 * @date: 2024/6/27
 * @Copyright: 黄嵘才（HuangRc)
 */
public class SiteDialog {

    SelectDialog<Site> dialog;

    public SiteDialog(Context context) {
        createDialogData(context);

    }

    private void createDialogData(Context context) {
        List<Site> sites = VodConfig.get().getSites();
        if (sites == null || sites.isEmpty()) {
            return;
        }
        this.dialog = new SelectDialog<>(context);
        dialog.setTip("请选择首页数据源");
        dialog.setAdapter(new SelectDialogAdapter.SelectDialogInterface<Site>() {
            @Override
            public void click(Site value, int pos) {
                VodConfig.get().setHome(value);

            }

            @Override
            public String getDisplay(Site val) {
                return val.getName();
            }
        }, new DiffUtil.ItemCallback<Site>() {
            @Override
            public boolean areItemsTheSame(@NonNull @NotNull Site oldItem, @NonNull @NotNull Site newItem) {
                return oldItem == newItem;
            }

            @Override
            public boolean areContentsTheSame(@NonNull @NotNull Site oldItem, @NonNull @NotNull Site newItem) {
                return oldItem.getKey().equals(newItem.getKey());
            }
        }, sites, sites.indexOf(VodConfig.get().getHome()));
    }

    public static SiteDialog create(Context context) {
        return new SiteDialog(context);
    }

    public void show() {
        dialog.show();
    }
}
